/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.command.component.serializer.CommandItem;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.web.command.component.ComponentConstant;
import com.inspur.edp.web.command.component.metadata.BranchCollectionCommandItem;
import com.inspur.edp.web.command.component.metadata.BranchCommandItem;
import com.inspur.edp.web.command.component.metadata.CommandItem;

import java.util.List;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class BranchCollectionCommandItemSerializer implements ICommandItemSerializer {
    @Override
    public JsonNode serialize(CommandItem item) {
        BranchCollectionCommandItem branchCollectionItem = (BranchCollectionCommandItem) item;
        JsonNode itemNode = (new ObjectMapper()).createObjectNode();
        JsonUtil.setPropValue(itemNode, ComponentConstant.CommandItemField.ITEM_TYPE, branchCollectionItem.getItemType());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.ID, branchCollectionItem.getId());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.CODE, branchCollectionItem.getCode());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.NAME, branchCollectionItem.getName());
        List<BranchCommandItem> items = branchCollectionItem.getItems();
        if (items == null || items.isEmpty()) {
            return itemNode;
        }
        ArrayNode itemsArrayNode = JsonUtil.addEmptyArray(itemNode, ComponentConstant.BranchCollectionCommandItemField.ITEMS);
        BranchCommandItemSerializer branchCommandItemSerializer = new BranchCommandItemSerializer();
        for (BranchCommandItem commandItem : items) {
            JsonNode commandItemNode = branchCommandItemSerializer.serialize(commandItem);
            itemsArrayNode.add(commandItemNode);
        }
        return itemNode;
    }

    @Override
    public CommandItem deserialize(JsonNode itemNode) {
        BranchCollectionCommandItem collectionItem = new BranchCollectionCommandItem();
        collectionItem.setId(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.ID, String.class));
        collectionItem.setCode(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.CODE, String.class));
        collectionItem.setName(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.NAME, String.class));

        ArrayNode itemsArrayNode = JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCollectionCommandItemField.ITEMS, ArrayNode.class);
        if (itemsArrayNode == null || itemsArrayNode.isEmpty()) {
            return collectionItem;
        }
        BranchCommandItemSerializer branchCommandItemSerializer = new BranchCommandItemSerializer();
        List<BranchCommandItem> items = collectionItem.getItems();
        for (JsonNode commandItemNode : itemsArrayNode) {
            BranchCommandItem commandItem = (BranchCommandItem) branchCommandItemSerializer.deserialize(commandItemNode);
            items.add(commandItem);
        }
        return collectionItem;
    }
}
